/*
 * Copyright (c) 2009 Nathaniel Houghton <nathan@brainwerk.org>
 *                    Robert Kirchgessner <rkirchge@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for
 * any purpose with or without fee is hereby granted, provided that
 * the above copyright notice and this permission notice appear in all
 * copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include "usart.h" 
#include "ui.h"
#include "ui.h"
#include "mode.h"
#include "cmd.h"
 
 
void
config_mode(struct mode *mode)
{
	struct cmd cmd;
	
	if (mode->state != STATE_FF_END) {
		if (ui_mode_change(mode))
			return;

		parse_line(&cmd);
		if (ser_mode_change(mode, &cmd))
			return;
	}
	
	switch (mode->state) {
	case STATE_0_INIT:
		menu(mode, 1);
		change_state(mode, 1);
		break;

	case 1:
		menu(mode, 0);
		config_mode_cmd(&cmd);
		break;

	case STATE_FF_END:
		break;
	}
}

void
config_mode_cmd(struct cmd *cmd)
{
	if (cmd->argc < 1)
		return;

	if (!strcasecmp(cmd->argv[0], "erase_nvm"))
		erase_nvm_cmd();
	else if (!strcasecmp(cmd->argv[0], "readn"))
		read_cmd(cmd);
	else if (!strcasecmp(cmd->argv[0], "reade"))
		read_cmd(cmd);
	else if (!strcasecmp(cmd->argv[0], "echo"))
		echo_cmd(cmd);
	else if (!strcasecmp(cmd->argv[0], "read_cfg"))
		read_cfg_cmd(cmd);
	else if (!strcasecmp(cmd->argv[0], "write_cfg"))
		write_cfg_cmd(cmd);
	else if (!strcasecmp(cmd->argv[0], "read_log"))
		read_log_cmd(cmd);
	else if (!strcasecmp(cmd->argv[0], "read_dtc"))
		num_dtc_cmd(cmd);
	else if (!strcasecmp(cmd->argv[0], "set_livemode"))
		set_livemode_cmd(cmd);
	else
		usart_puts(0, "Invalid command.\r\n");
}
